package com.huatai.verity.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huatai.common.api.R;
import com.huatai.verity.entity.AuthObject;
import com.huatai.verity.service.AuthObjectService;
import com.huatai.verity.vo.AuthObjectParmVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @program: ht-system
 * @description: 授权对象控制器
 * @author: songxiude
 * @create: 2023-07-21 16:25
 **/
@RestController
@RequestMapping("/authobject")
@Api(value = "授权对象接口",tags = "授权对象接口")
public class AuthObjectController {
	@Autowired
	private AuthObjectService authObjectService;

	@PostMapping("/saveUpdate")
	@ApiOperation(value = "新增修改授权对象",notes = "新增时可以不用传id")
	public R saveUpdate(@Valid @RequestBody AuthObject authObject){
		  return R.status(authObjectService.saveUpdate(authObject));
	}

	@PostMapping("/delete")
	@ApiOperation(value = "批量删除授权对象",notes = "例子：[11111,22222]")
	public R delete(@RequestBody List<Long> ids){
		if (ids.isEmpty()) {
			return R.fail("id不能为空");
		}
		return R.status(authObjectService.removeBatchByIds(ids));
	}

	@PostMapping("/getAuthObjects")
	@ApiOperation(value = "获取授权对象列表",notes = "支撑授权对象和单位名称、手机号模糊查询")
	public R getAuthObjects(@RequestBody AuthObjectParmVo authObjectParmVo){
		return R.data(authObjectService.getAuthObjects(authObjectParmVo));
	}

	@GetMapping("/getAll")
	@ApiOperation(value = "获取全部授权对象列表",notes = "获取全部授权对象列表")
	public R<List<AuthObject>> getAll(){
		LambdaQueryWrapper<AuthObject> wrapper = Wrappers.<AuthObject>lambdaQuery().orderByDesc(AuthObject::getCreateTime);
		List<AuthObject> list = Optional.ofNullable(authObjectService.list(wrapper)).orElse(Collections.emptyList());
		return R.data(list);
	}
}
